import axios from "axios";
import Router from "vue-router";
import {Message} from "element-ui";
// axios.defaults.headers.common['token'] = '' // 可以设置post,get或者common中的字段

const  instance  =axios.create({
  timeout:5000
})
instance.interceptors.request.use(
  function (config){
    console.group("全局请求拦截");
    console.log(config)
    console.groupEnd();
    if (localStorage.getItem("token")){
      config.headers['token']= localStorage.getItem("token")
    }
    return config
  },
    function(err){
    return Promise.reject(err);
    }
  )

instance.interceptors.response.use(
  function (response){
    console.group("全局响应拦截");
    console.log(response)
    console.groupEnd();
    if (response.data.tokenCode){
      Message.error('用户消息已失效或不合法，请重新登录');
      localStorage.removeItem("token")
      window.location.href='/'
    }
    return response;
  },
  function (error){
    return Promise.reject(error);
  }
)


export function get(url,params){
  return instance.get(url,{
    params
  })
}
export function post(url,data){
  return instance.post(url,data)
}


